package com.frame.database;

import java.io.Serializable;
import java.util.*;

import net.sf.cglib.proxy.Enhancer;

import com.frame.database.util.ConditionMap;
import com.frame.database.util.Seq;
import com.frame.database.operate.JdbcDao;
import com.frame.database.operate.Jsession;
import com.frame.database.proxy.JDaoProxy;
import com.frame.database.session.JdbcSession;
import com.frame.util.SelectItem;

/**
 * @Title: 系统
 * @Description:
 * @Copyright: Copyright (c) 2015
 * @Company:
 * @author 王海锋
 * @CreatedTime:2015-6-25 下午02:41:40
 * @version 1.0
 */

abstract public class JBaseDao implements JdbcDao{

    private String tbName;
    private String keyCol;
    private String dbFtg;
    private Jsession session;

    public JBaseDao(String tbName){
        this.tbName=tbName;
        this.keyCol="id";
    }
    public JBaseDao(String tbName,String keyCol){
        this.tbName=tbName;
        this.keyCol=keyCol;
        if(this.keyCol==null || "".equals(this.keyCol))
            this.keyCol="id";
    }
    public JBaseDao(String tbName,String keyCol,String dbFtg){
        this.tbName=tbName;
        this.keyCol=keyCol;
        this.dbFtg=dbFtg;
    }

    public List<JdataBean> query(ConditionMap paramMap) throws Exception{
        String sql="select * from "+tbName+" t0";
        String whereSql="";
        String orderSql="";
        List parmlist=new ArrayList();
        if(paramMap!=null){
            for(int i=0;i<paramMap.size();i++){
                if(whereSql.equals(""))
                    whereSql+=" where "+paramMap.getCondition(i);
                else
                    whereSql+=" and "+paramMap.getCondition(i);
            }
            List olist=paramMap.getOrder();
            if(olist!=null){
                for(int i=0;i<olist.size();i++){
                    SelectItem item=(SelectItem)olist.get(i);
                    if(orderSql.equals(""))
                        orderSql+=" order by t0."+item.getValue()+" "+item.getText();
                    else
                        orderSql+=",t0."+item.getValue()+" "+item.getText();
                }
            }
        }
        if(paramMap!=null){
            for(int i=0;i<paramMap.paramSize();i++){
                parmlist.add(paramMap.getParamObj(i));
            }
        }
        sql+=whereSql+orderSql;
        return getSession().query2(sql, parmlist);
    }
    public PageResult queryPage(ConditionMap paramMap,int pageSize,int pageNo) throws Exception{
        String sql="select * from "+tbName+" t0";
        String whereSql="";
        String orderSql="";
        List parmlist=new ArrayList();
        if(paramMap!=null){
            for(int i=0;i<paramMap.size();i++){
                if(whereSql.equals(""))
                    whereSql+=" where "+paramMap.getCondition(i);
                else
                    whereSql+=" and "+paramMap.getCondition(i);
            }
            List olist=paramMap.getOrder();
            if(olist!=null){
                for(int i=0;i<olist.size();i++){
                    SelectItem item=(SelectItem)olist.get(i);
                    if(orderSql.equals(""))
                        orderSql+=" order by t0."+item.getValue()+" "+item.getText();
                    else
                        orderSql+=",t0."+item.getValue()+" "+item.getText();
                }
            }
        }
        if(paramMap!=null){
            for(int i=0;i<paramMap.paramSize();i++){
                parmlist.add(paramMap.getParamObj(i));
            }
        }
        sql+=whereSql+orderSql;
        return getSession().query2(sql, parmlist, pageSize, pageNo);
    }

    public void save(Map dataMap) throws Exception{
        Object obj=dataMap.get(keyCol.toLowerCase());
        if(obj==null || "".equals(obj.toString()))
            dataMap.put(keyCol, new Seq().getKeyId(tbName.toUpperCase()+"_ID"));
        getSession().save(tbName, dataMap);
    }
    public void update(Map updateMap,Map keyMap) throws Exception{
        getSession().update(tbName, updateMap,keyMap);
    }
    public void del(Serializable keyid) throws Exception{
        getSession().delete("delete from "+tbName+" where "+keyCol+"=?", new Object[]{keyid});
    }
    public JdataBean getBean(Serializable keyid) throws Exception{
        String sql="select * from "+tbName+" where "+keyCol+"=?";
        List<JdataBean> list=getSession().query(sql, new Object[]{keyid});
        if(list.size()>0)
            return list.get(0);
        else
            return null;
    }
    public Jsession getSession() {
        // TODO Auto-generated method stub
        if(session==null){
//            session=new JdbcSession(dbFtg);
            session=(Jsession)Enhancer.create(JdbcSession.class, new JDaoProxy(dbFtg));
        }
        return session;
    }
    public String getTbName() {
        return tbName;
    }
    public void setTbName(String tbName) {
        this.tbName = tbName;
    }
    public String getKeyCol() {
        return keyCol;
    }
    public void setKeyCol(String keyCol) {
        this.keyCol = keyCol;
    }
    public String getDbFtg() {
        return dbFtg;
    }
    public void setDbFtg(String dbFtg) {
        this.dbFtg = dbFtg;
    }

}
